package sprinklerbee.views;

import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.border.TitledBorder;

import sprinklerbee.model.Configuration;
import sprinklerbee.model.SensorRealTime;
import sprinklerbee.utils.MemoryCache;

import java.awt.SystemColor;
import javax.swing.border.EtchedBorder;
import java.awt.Font;

@SuppressWarnings("serial")
public class SensorPanel extends JPanel {
	private JLabel clock_val;
	private static JCheckBox rain_val;
	private static JLabel lblTempval;
	private static JLabel lbl_recordtime;

	public SensorPanel() {

		javax.swing.Timer t = new javax.swing.Timer(1000, new ClockListener());
		t.start();

		setLayout(new GridLayout(2, 6, 5, 5));
		setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, null,
				null), "Sensor information"));
		setBackground(SystemColor.menu);
		setPreferredSize(new Dimension(850, 100));

		JPanel panel = new JPanel();
		add(panel);
		panel.setLayout(new GridLayout(0, 6, 0, 0));

		JLabel lbl_temp = new JLabel();
		panel.add(lbl_temp);
		lbl_temp.setText("Temperature:");

		lblTempval = new JLabel("0");
		lblTempval.setFont(new Font("Tahoma", Font.BOLD, 16));
		panel.add(lblTempval);

		JLabel lbl_rain = new JLabel();
		panel.add(lbl_rain);
		lbl_rain.setText("Rain:");

		rain_val = new JCheckBox();
		rain_val.setEnabled(false);
		panel.add(rain_val);

		JPanel panel_1 = new JPanel();
		add(panel_1);
		panel_1.setLayout(new GridLayout(1, 5, 0, 0));

		JLabel lblNewLabel = new JLabel("Record time:");
		lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 16));
		panel_1.add(lblNewLabel);

		lbl_recordtime = new JLabel("");
		panel_1.add(lbl_recordtime);
		lbl_recordtime.setFont(new Font("Tahoma", Font.BOLD, 16));

		JLabel lbl_clock = new JLabel();
		lbl_clock.setFont(new Font("Tahoma", Font.BOLD, 16));
		panel_1.add(lbl_clock);
		lbl_clock.setText("Clock:");
		clock_val = new JLabel();
		clock_val.setForeground(new Color(50, 205, 50));
		panel_1.add(clock_val);
		clock_val.setFont(new Font("Arial", Font.BOLD, 18));

		clock_val.setBackground(SystemColor.menu);

		updateSensor();

	}

	// Shows the clock
	class ClockListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			clock_val.setText(new SimpleDateFormat("MM/dd/yyyy HH:mm:ss")
					.format(new Date()));
		}
	}

	public static void updateSensor() {
		SensorRealTime sensorRealTime = (SensorRealTime) MemoryCache.getMemoryCache().get("sensorRealTime");
		Map<String, Configuration> configurations=(Map<String, Configuration>) MemoryCache.getMemoryCache().get("configurations");
		lblTempval.setText(String.valueOf(sensorRealTime.getTemperature()));
		lbl_recordtime.setText(new SimpleDateFormat("MM/dd/yyyy HH:mm").format(sensorRealTime.getCreatetime()));
		if (sensorRealTime.getTemperature()>Double.parseDouble(configurations.get("maxtemp").getValue())) {
			lblTempval.setForeground(new Color(255, 69, 0));
		}else if (sensorRealTime.getTemperature()<Double.parseDouble(configurations.get("mintemp").getValue())) {
			lblTempval.setForeground(new Color(135, 206, 235));
		}else{
			lblTempval.setForeground(new Color(0, 0, 0));
		}
		if (String.valueOf(sensorRealTime.getRain()).equals("0.0")) {
			rain_val.setSelected(false);
		} else {
			rain_val.setSelected(true);
		}

	}
}
